<template>
  <div style="padding-top: 10px">
    <span>当前版本 {{ version }}</span>
    <a-spin :spinning="spinning">
      <a-tabs default-active-key="1">
        <a-tab-pane key="1" tab="上传文件升级" :disabled="!isEdge">
          <a-upload
            name="file"
            ref="uploadCtrl"
            action="api/upload/"
            :showUploadList="false"
            @change="handleFileChange"
            :multiple="false"
          >
            <a-button> <a-icon type="upload" />上传升级包</a-button>
          </a-upload>
          <p></p>
          <span v-if="isUploaded">文件名:{{ curFileName }}</span>
          <p></p>
          <div v-if="isUploaded">
            节点用户名:<a-input v-model="un" style="width: 200px"></a-input>
            节点密码:<a-input v-model="pwd" style="width: 200px"></a-input>
            <a-button @click="upgrade">升级</a-button>
          </div>
          <a-textarea :rows="20" v-model="result"></a-textarea>
        </a-tab-pane>
        <a-tab-pane key="2" tab="使用http服务器升级">
          <!--
          服务器用户名:<a-input
            style="width: 100px; margin-left: 30px; margin-right: 20px"
            v-model="user"
          ></a-input>
          服务器密码:<a-input
            style="width: 100px; margin-left: 10px"
            v-model="pswd"
          ></a-input>
          <p />-->
          安装包地址
          <!--<a-select
            v-model="netType"
            placeholder="Please select"
            style="width: 100px"
          >
            <a-select-option value="http://">http://</a-select-option>
          </a-select>-->

          <a-input v-model="fileAddr" style="width: 600px"></a-input>
          <a-button @click="upgradeByRemoteAddr" v-if="isEdge">升级</a-button>
          <a-button @click="upgradeEdgesByRemoteAddr" v-else
            >升级全部节点</a-button
          >
          <p></p>
          升级过程：
          <a-textarea :rows="20" v-model="result"></a-textarea>
        </a-tab-pane>
      </a-tabs>
    </a-spin>
  </div>
</template>
<script>
import { postEx, getEx } from "@/services/crud";
export default {
  data() {
    return {
      isUploaded: false,
      curFileName: "",
      result: "",
      fileList: [],
      spinning: false,
      timerID: null,
      version: "",
      netType: "http://",
      fileAddr: "",
      user: "kdl",
      pswd: "kdl123",
      un: "linaro",
      pwd: "linaro",
      stateTimerID: null,
      upGradeResult: "",
      upgradeSpinning: false,
    };
  },
  mounted() {
    this.refresh();
    this.beginTimer();
  },
  beforeRouteLeave(to, from, next) {
    // 导航离开该组件的对应路由时调用
    // 可以访问组件实例 `this`
    console.log("***********kill timer****", process.env.VUE_APP_TYPE);
    this.killTimer();
    next();
  },
  computed: {
    isEdge() {
      return process.env.VUE_APP_TYPE === "edge";
    },
  },
  methods: {
    beginTimer() {
      let self = this;
      this.killTimer();
      this.timerID = setInterval(() => {
        self.refresh();
      }, 5000);
    },
    killTimer() {
      if (this.timerID) {
        clearInterval(this.timerID);
        this.timerID = null;
      }
      if (this.stateTimerID) {
        clearInterval(this.stateTimerID);
        this.stateTimerID = null;
      }
    },
    refresh() {
      let self = this;

      getEx("/ver").then((res) => {
        if (res && res.data && res.data.result) {
          self.version = res.data.result;
        }
      }).catch(err=>{
        console.log("error--------",err)
      });
    },
    handleFileChange(info) {
      if (info.file.status === "done") {
        this.spinning = false;
        this.$message.success("版本上传成功");
        if (
          info.file.response &&
          info.file.response.result &&
          info.file.response.result.length == 1
        )
          this.curFileName = info.file.response.result[0];
        this.isUploaded = true;
        //this.fileList = []
      } else if (info.file.status === "error") {
        this.$message.error(`版本上传失败`);
        this.isUploaded = false;
        this.spinning = false;
      } else {
        this.isUploaded = false;
        this.spinning = true;
      }
      console.log("*************", info);
    },
    getUpdateInfo() {
      if (this.stateTimerID) {
        return;
      }
      let self = this;
      this.result = "";
      this.stateTimerID = setInterval(() => {
        getEx("/upgrade/").then((res) => {
          if (res && res.data && res.data.result) {
            self.result = res.data.result;
          }
        });
      }, 8000);
    },
    upgrade() {
      let self = this;
      self.result = "";
      if (this.curFileName) {
        postEx("/upgrade/", {
          file_name: this.curFileName,
          un: self.un,
          password: self.pwd,
        })
          .then((res) => {
            if (res && res.data && res.data.result) {
              let result = res.data.result;
              console.log("&&&&&&&&&^^^^^^^^^^^^", result);
              self.$message.info("升级开始");
              self.getUpdateInfo();
            }
          })
          .catch((err) => {
            this.$message.error("升级失败：" + err.error.message);
            self.result = "升级失败 错误信息 " + err.error.message;
          });
      }
    },
    upgradeByRemoteAddr() {
      let addr = this.netType + this.fileAddr;
      if(addr.indexOf("http://") == -1){
        addr = "http://" + addr
      }
      console.log("&&&&&&&&&&&&&&&&&&", addr);
      if (addr && addr.length > 5) {
        let self = this;
        let param = this.netType == "ftp://" ? this.user + ":" + this.pswd : "";
        postEx("/upgrade/", {
          file_name: addr,
          param: param,
          un: this.un,
          password: this.pwd,
        })
          .then((res) => {
            if (res && res.data && res.data.result) {
              let result = res.data.result;
              console.log("&&&&&&&&&^^^^^^^^^^^^", result);
              self.$message.info("升级开始");
              self.getUpdateInfo();
            }
          })
          .catch((err) => {
            self.$message.error("升级失败：" + err.error.message);
            //self.result = "升级失败 错误信息 " + err.error.message;
          });
      }
    },
    upgradeEdgesByRemoteAddr() {
      let addr = this.netType + this.fileAddr;
      console.log("&&&&&&&&&&&&&&&&&&22222", addr);
      if (addr && addr.length > 5) {
        let param = "-u " + this.user + ":" + this.pswd;
        postEx("/upgradeEdges/all", {
          file_name: addr,
          param: param,
          un: this.un,
          password: this.pwd,
        })
          .then((res) => {
            if (res && res.data && res.data.result) {
              let result = res.data.result;
              console.log("&&&&&&&&&^^^^^^^^^^^^", result);
            }
          })
          .catch((err) => {
            self.$message.error("升级失败：");
            self.result = "升级失败 错误信息 " + err.error.message;
          });
      }
    },
  },
};
</script>
<style scoped lang="less">
/deep/.ant-upload-list-item-name {
  color: #3dc0ef;
}
</style>